package cn.ncist.health_backend.handler;

import cn.ncist.common.RespResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        Result result=new Result(true,"登录成功");
        RespResult respResult = new RespResult(true, "登录成功");
        //设置响应头
        response.setContentType("application/json;charset=utf-8");
        //从上下文中取认证信息
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();

        //认证信息存到redis........

        //restult转json
        response.getWriter().write(new ObjectMapper().writeValueAsString(respResult));
    }
}
